﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Dry.DbInterpreter;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Dry.Generators
{
    public class AspxGenerator : GeneratorBase
    {
        private static string _tablePrefix;

        public static GeneratorFeedback CreateFile(string strTableName, string strMasterpageName, string tablePrefix)
        {
            GeneratorFeedback generatorFeedback = new GeneratorFeedback();

            _tablePrefix = tablePrefix;

            int lines = 0;

            string pageName = strTableName.Substring(tablePrefix.Length, strTableName.Length - tablePrefix.Length);

            // The .Master file
            string strCode = GetAspxFile(strTableName, strMasterpageName);
            generatorFeedback.Exception = CreateFileFromString("Private\\AutoGenerated\\" + pageName + ".aspx", strCode);
            lines += CountLinesInString(strCode);

            // The .Master.Designer.cs file
            strCode = GetAspxDesignerFile(strTableName);
            generatorFeedback.Exception = CreateFileFromString("Private\\AutoGenerated\\" + pageName + ".aspx.designer.cs", strCode);
            lines += CountLinesInString(strCode);

            // The Master.cs file
            strCode = GetAspxClass(strTableName);
            generatorFeedback.Exception = CreateFileFromString("Private\\AutoGenerated\\" + pageName + ".aspx.cs", strCode);
            lines += CountLinesInString(strCode);

            generatorFeedback.Lines = lines;

            return generatorFeedback;
        }

        public static GeneratorFeedback CreateBasePage()
        {
            GeneratorFeedback generatorFeedback = new GeneratorFeedback();

            string strCode = GetBasePage();
            generatorFeedback.Exception = CreateFileFromString("Private\\BasePage.cs", strCode);
            generatorFeedback.Lines = CountLinesInString(strCode);

            return generatorFeedback;
        }

        public static GeneratorFeedback CreateDefaultPage(string strMasterpageName, List<string> tableNames)
        {
            GeneratorFeedback generatorFeedback = new GeneratorFeedback();

            string strCode = GetDefaultAspxFile("Default", strMasterpageName, tableNames);
            generatorFeedback.Exception = CreateFileFromString("Default.aspx", strCode);
            generatorFeedback.Lines = CountLinesInString(strCode);

            strCode = GetDefaultAspxClass("Default");
            generatorFeedback.Exception = CreateFileFromString("Default.aspx.cs", strCode);
            generatorFeedback.Lines = CountLinesInString(strCode);

            strCode = GetDefaultAspxDesignerFile("Default");
            generatorFeedback.Exception = CreateFileFromString("Default.aspx.designer.cs", strCode);
            generatorFeedback.Lines = CountLinesInString(strCode);

            return generatorFeedback;
        }

        private static string GetBasePage()
        {
            string strCode = String.Empty;

            strCode += "using System;" + _strNewLine;
            strCode += "using System.Collections.Generic;" + _strNewLine;
            strCode += "using System.Globalization;" + _strNewLine;
            strCode += "using System.Linq;" + _strNewLine;
            strCode += "using System.Threading;" + _strNewLine;
            strCode += "using System.Web;" + _strNewLine;
            strCode += "using System.Web.UI;" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "namespace " + Dry.ProjectName + "" + _strNewLine;
            strCode += "{" + _strNewLine;
            strCode += "    public class BasePage : Page" + _strNewLine;
            strCode += "    {" + _strNewLine;
            strCode += "    " + _strNewLine;
            strCode += "    }" + _strNewLine;
            strCode += "}" + _strNewLine;

            return strCode;
        }

        public static string GetDefaultAspxFile(string strTableName, string strMasterpageName, List<string> tableNames)
        {
            string strCode = String.Empty;
            strCode += "<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"Default.aspx.cs\" Inherits=\"" + Dry.ProjectName + ".Default\" MasterPageFile=\"~/MasterPages/AutoGenerated/" + strMasterpageName + ".Master\" %>" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "<asp:Content ID=\"cntHead\" ContentPlaceHolderID=\"cphHead\" runat=\"server\">" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "</asp:Content>" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "<asp:Content ID=\"cntHeader\" ContentPlaceHolderID=\"cphHeader\" runat=\"server\">" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "<h1>Default</h1>" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "</asp:Content>" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "<asp:Content ID=\"cntMenu\" ContentPlaceHolderID=\"cphMenu\" runat=\"server\">" + _strNewLine;
            strCode += "" + _strNewLine;

            foreach (string tableName in tableNames)
            {
                DbTable dbTable = new DbTable(tableName, _tablePrefix);

                strCode += "    <ul>" + _strNewLine;
                strCode += "        <li>" + _strNewLine;
                strCode += "            <a href=\"Private/AutoGenerated/" + dbTable.ClassName + ".aspx\"><b>" + dbTable.ClassName + "</b></a>" + _strNewLine;
                strCode += "        </li>" + _strNewLine;
                strCode += "    </ul>" + _strNewLine;
            }

            // TODO Add code for default page
            strCode += "" + _strNewLine;
            strCode += "</asp:Content>" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "<asp:Content ID=\"cntContent\" ContentPlaceHolderID=\"cphContent\" runat=\"server\">" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "</asp:Content>" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "<asp:Content ID=\"cntFooter\" ContentPlaceHolderID=\"cphFooter\" runat=\"server\">" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "</asp:Content>" + _strNewLine;

            return strCode;
        }

        public static string GetAspxFile(string strTableName, string strMasterpageName)
        {
            string strCode = String.Empty;
            DbTable dbTable = new DbTable(strTableName, _tablePrefix);

            strCode += "<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"" + dbTable.ClassName + ".aspx.cs\" Inherits=\"" + Dry.ProjectName + "." + dbTable.ClassName + "\" MasterPageFile=\"~/MasterPages/AutoGenerated/" + strMasterpageName + ".Master\" %>" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "<%@ Register Src=\"~/UserControls/AutoGenerated/uc" + dbTable.ClassName + "Edit.ascx\" TagName=\"" + dbTable.ClassName + "Edit\" TagPrefix=\"uc\" %>" + _strNewLine;
            strCode += "<%@ Register Src=\"~/UserControls/AutoGenerated/uc" + dbTable.ClassName + "List.ascx\" TagName=\"" + dbTable.ClassName + "List\" TagPrefix=\"uc\" %>" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "" + _strNewLine;

            // Head area
            strCode += "<asp:Content ID=\"cntHead\" ContentPlaceHolderID=\"cphHead\" runat=\"server\">" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "</asp:Content>" + _strNewLine;
            strCode += "" + _strNewLine;

            // Header area
            strCode += "<asp:Content ID=\"cntHeader\" ContentPlaceHolderID=\"cphHeader\" runat=\"server\">" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "</asp:Content>" + _strNewLine;
            strCode += "" + _strNewLine;

            // Menu area
            strCode += "<asp:Content ID=\"cntMenu\" ContentPlaceHolderID=\"cphMenu\" runat=\"server\">" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "</asp:Content>" + _strNewLine;
            strCode += "" + _strNewLine;

            // Content area
            // Generated by dryify
            strCode += "<asp:Content ID=\"cntContent\" ContentPlaceHolderID=\"cphContent\" runat=\"server\">" + _strNewLine;
            strCode += "    <h1>" + dbTable.ClassName + "</h1>" + _strNewLine;
            strCode += "    <% // Edit/add %>" + _strNewLine;
            strCode += "    <asp:LinkButton ID=\"lbnAddNew" + dbTable.ClassName + "\" runat=\"server\" Text=\"Add new\" OnClick=\"lbnAddNew" + dbTable.ClassName + "_Click\" />" + _strNewLine;
            strCode += "    " + _strNewLine;
            strCode += "    <asp:Panel ID=\"pnlEdit" + dbTable.ClassName + "\" runat=\"server\" Visible=\"false\">" + _strNewLine;
            strCode += "        <uc:" + dbTable.ClassName + "Edit ID=\"uc" + dbTable.ClassName + "Edit\" runat=\"server\" />" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "        <br />" + _strNewLine;
            strCode += "        <asp:LinkButton ID=\"lbnSave" + dbTable.ClassName + "\" runat=\"server\" Text=\"Save\" OnClick=\"lbnSave" + dbTable.ClassName + "_Click\" />" + _strNewLine;
            strCode += "        <br />" + _strNewLine;
            strCode += "        <asp:LinkButton ID=\"lbnCancelSave" + dbTable.ClassName + "\" runat=\"server\" Text=\"Cancel\" OnClick=\"lbnCancelSave" + dbTable.ClassName + "_Click\" />" + _strNewLine;
            strCode += "    </asp:Panel>" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "    <asp:Panel ID=\"pnl" + dbTable.ClassName + "Saved\" runat=\"server\" Visible=\"false\" EnableViewState=\"false\">" + _strNewLine;
            strCode += "        <i>The user has been saved!</i>" + _strNewLine;
            strCode += "    </asp:Panel>" + _strNewLine;
            strCode += "    " + _strNewLine;
            strCode += "    <% // List %>" + _strNewLine;
            strCode += "    <asp:Panel ID=\"pnl" + dbTable.ClassName + "List\" runat=\"server\">" + _strNewLine;
            strCode += "        <uc:" + dbTable.ClassName + "List ID=\"uc" + dbTable.ClassName + "List\" runat=\"server\" />" + _strNewLine;
            strCode += "    </asp:Panel>" + _strNewLine;
            strCode += "</asp:Content>" + _strNewLine;

            strCode += "" + _strNewLine;

            // Footer area
            strCode += "<asp:Content ID=\"cntFooter\" ContentPlaceHolderID=\"cphFooter\" runat=\"server\">" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "</asp:Content>" + _strNewLine;

            return strCode;
        }

        private static string AddDesignerControlCode(string controlNamespace, string controlType, string controlName)
        {
            string strCode = string.Empty;
            //DbTable dbTable = new DbTable(strTableName, _tablePrefix);

            strCode += "    /// <summary>" + _strNewLine;
            strCode += "    /// head control." + _strNewLine;
            strCode += "    /// </summary>" + _strNewLine;
            strCode += "    /// <remarks>" + _strNewLine;
            strCode += "    /// Auto-generated field." + _strNewLine;
            strCode += "    /// To modify move field declaration from designer file to code-behind file." + _strNewLine;
            strCode += "    /// </remarks>" + _strNewLine;
            strCode += "    protected global::" + controlNamespace + "." + controlType + " " + controlName + ";" + _strNewLine;

            return strCode;
        }

        public static string GetAspxDesignerFile(string strTableName)
        {
            string strCode = string.Empty;
            DbTable dbTable = new DbTable(strTableName, _tablePrefix);

            strCode += "//------------------------------------------------------------------------------" + _strNewLine;
            strCode += "// <auto-generated>" + _strNewLine;
            strCode += "//     This code was generated by a tool." + _strNewLine;
            strCode += "//" + _strNewLine;
            strCode += "//     Changes to this file may cause incorrect behavior and will be lost if" + _strNewLine;
            strCode += "//     the code is regenerated. " + _strNewLine;
            strCode += "// </auto-generated>" + _strNewLine;
            strCode += "//------------------------------------------------------------------------------" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "namespace " + Dry.ProjectName + " {" + _strNewLine;
            strCode += "    " + _strNewLine;
            strCode += "    " + _strNewLine;
            strCode += "        public partial class " + dbTable.ClassName + " {" + _strNewLine;
            strCode += "                " + _strNewLine;

            // Generated by dryify
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// lbnAddNew" + dbTable.ClassName + " control." + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        /// <remarks>" + _strNewLine;
            strCode += "        /// Auto-generated field." + _strNewLine;
            strCode += "        /// To modify move field declaration from designer file to code-behind file." + _strNewLine;
            strCode += "        /// </remarks>" + _strNewLine;
            strCode += "        protected global::System.Web.UI.WebControls.LinkButton lbnAddNew" + dbTable.ClassName + ";" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// pnlEdit" + dbTable.ClassName + " control." + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        /// <remarks>" + _strNewLine;
            strCode += "        /// Auto-generated field." + _strNewLine;
            strCode += "        /// To modify move field declaration from designer file to code-behind file." + _strNewLine;
            strCode += "        /// </remarks>" + _strNewLine;
            strCode += "        protected global::System.Web.UI.WebControls.Panel pnlEdit" + dbTable.ClassName + ";" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// uc" + dbTable.ClassName + "Edit control." + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        /// <remarks>" + _strNewLine;
            strCode += "        /// Auto-generated field." + _strNewLine;
            strCode += "        /// To modify move field declaration from designer file to code-behind file." + _strNewLine;
            strCode += "        /// </remarks>" + _strNewLine;
            strCode += "        protected global::Frontend.UserControls.uc" + dbTable.ClassName + "Edit uc" + dbTable.ClassName + "Edit;" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// lbnSave" + dbTable.ClassName + " control." + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        /// <remarks>" + _strNewLine;
            strCode += "        /// Auto-generated field." + _strNewLine;
            strCode += "        /// To modify move field declaration from designer file to code-behind file." + _strNewLine;
            strCode += "        /// </remarks>" + _strNewLine;
            strCode += "        protected global::System.Web.UI.WebControls.LinkButton lbnSave" + dbTable.ClassName + ";" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// lbnCancelSave" + dbTable.ClassName + " control." + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        /// <remarks>" + _strNewLine;
            strCode += "        /// Auto-generated field." + _strNewLine;
            strCode += "        /// To modify move field declaration from designer file to code-behind file." + _strNewLine;
            strCode += "        /// </remarks>" + _strNewLine;
            strCode += "        protected global::System.Web.UI.WebControls.LinkButton lbnCancelSave" + dbTable.ClassName + ";" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// pnl" + dbTable.ClassName + "Saved control." + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        /// <remarks>" + _strNewLine;
            strCode += "        /// Auto-generated field." + _strNewLine;
            strCode += "        /// To modify move field declaration from designer file to code-behind file." + _strNewLine;
            strCode += "        /// </remarks>" + _strNewLine;
            strCode += "        protected global::System.Web.UI.WebControls.Panel pnl" + dbTable.ClassName + "Saved;" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// pnl" + dbTable.ClassName + "List control." + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        /// <remarks>" + _strNewLine;
            strCode += "        /// Auto-generated field." + _strNewLine;
            strCode += "        /// To modify move field declaration from designer file to code-behind file." + _strNewLine;
            strCode += "        /// </remarks>" + _strNewLine;
            strCode += "        protected global::System.Web.UI.WebControls.Panel pnl" + dbTable.ClassName + "List;" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// uc" + dbTable.ClassName + "List control." + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        /// <remarks>" + _strNewLine;
            strCode += "        /// Auto-generated field." + _strNewLine;
            strCode += "        /// To modify move field declaration from designer file to code-behind file." + _strNewLine;
            strCode += "        /// </remarks>" + _strNewLine;
            strCode += "        protected global::Frontend.UserControls.uc" + dbTable.ClassName + "List uc" + dbTable.ClassName + "List;" + _strNewLine;


            strCode += "        }" + _strNewLine;
            strCode += "}" + _strNewLine;

            return strCode;
        }

        public static string GetDefaultAspxDesignerFile(string strTableName)
        {
            string strCode = string.Empty;

            strCode += "//------------------------------------------------------------------------------" + _strNewLine;
            strCode += "// <auto-generated>" + _strNewLine;
            strCode += "//     This code was generated by a tool." + _strNewLine;
            strCode += "//" + _strNewLine;
            strCode += "//     Changes to this file may cause incorrect behavior and will be lost if" + _strNewLine;
            strCode += "//     the code is regenerated. " + _strNewLine;
            strCode += "// </auto-generated>" + _strNewLine;
            strCode += "//------------------------------------------------------------------------------" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "namespace " + Dry.ProjectName + " {" + _strNewLine;
            strCode += "    " + _strNewLine;
            strCode += "    " + _strNewLine;
            strCode += "        public partial class " + strTableName + " {" + _strNewLine;
            strCode += "                " + _strNewLine;

            strCode += "        }" + _strNewLine;
            strCode += "}" + _strNewLine;

            return strCode;
        }

        public static string GetDefaultAspxClass(string pageName)
        {
            string strCode = String.Empty;

            strCode += GetFileHeader(Dry.ProjectName, "", "", "", "", "");

            strCode += "        using System;" + _strNewLine;
            strCode += "        using System.Collections.Generic;" + _strNewLine;
            strCode += "        using System.Linq;" + _strNewLine;
            strCode += "        using System.Web;" + _strNewLine;
            strCode += "        using System.Web.UI;" + _strNewLine;
            strCode += "        using System.Web.UI.WebControls;" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        namespace " + Dry.ProjectName + _strNewLine;
            strCode += "        {" + _strNewLine;
            strCode += "            public partial class " + pageName + " : BasePage" + _strNewLine;
            strCode += "            {" + _strNewLine;
            strCode += "                protected void Page_Load(object sender, EventArgs e)" + _strNewLine;
            strCode += "                {" + _strNewLine;
            strCode += "                }" + _strNewLine;
            strCode += "            }" + _strNewLine;
            strCode += "        }" + _strNewLine;

            return strCode;
        }

        public static string GetAspxClass(string strTableName)
        {
            string strCode = String.Empty;
            DbTable dbTable = new DbTable(strTableName, _tablePrefix);

            strCode += GetFileHeader(Dry.ProjectName, "", "", "", "", "");

            // Generated by dryify
            strCode += "using System;" + _strNewLine;
            strCode += "using System.Collections.Generic;" + _strNewLine;
            strCode += "using System.Linq;" + _strNewLine;
            strCode += "using System.Web;" + _strNewLine;
            strCode += "using System.Web.UI;" + _strNewLine;
            strCode += "using System.Web.UI.WebControls;" + _strNewLine;
            strCode += "using Backend.BLL;" + _strNewLine;
            strCode += "using Backend.IP;" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "namespace Frontend" + _strNewLine;
            strCode += "{" + _strNewLine;
            strCode += "    public partial class " + dbTable.ClassName + " : BasePage" + _strNewLine;
            strCode += "    {" + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// " + dbTable.ClassName + " id of the current user if one is selected" + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        public int? " + dbTable.ClassName + "Id" + _strNewLine;
            strCode += "        {" + _strNewLine;
            strCode += "            get { return ViewState[\"" + dbTable.ClassName + "Id\"] != null ? int.Parse(ViewState[\"" + dbTable.ClassName + "Id\"].ToString()) : (int?)null; }" + _strNewLine;
            strCode += "            set { ViewState[\"" + dbTable.ClassName + "Id\"] = value; }" + _strNewLine;
            strCode += "        }" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        protected void Page_Load(object sender, EventArgs e)" + _strNewLine;
            strCode += "        {" + _strNewLine;
            strCode += "            if(!IsPostBack)" + _strNewLine;
            strCode += "            {" + _strNewLine;
            strCode += "                Update" + dbTable.ClassName + "List();" + _strNewLine;
            strCode += "            }" + _strNewLine;
            strCode += "            " + _strNewLine;
            strCode += "            uc" + dbTable.ClassName + "List.Command += uc" + dbTable.ClassName + "List_Command;" + _strNewLine;
            strCode += "        }" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        void uc" + dbTable.ClassName + "List_Command(object sender, GridViewCommandEventArgs e)" + _strNewLine;
            strCode += "        {" + _strNewLine;
            strCode += "            switch (e.CommandName)" + _strNewLine;
            strCode += "            {" + _strNewLine;
            strCode += "                case \"View" + dbTable.ClassName + "\":" + _strNewLine;
            strCode += "                    break;" + _strNewLine;
            strCode += "                " + _strNewLine;
            strCode += "                case \"Edit" + dbTable.ClassName + "\":" + _strNewLine;
            strCode += "                    OpenEditPanel();" + _strNewLine;
            strCode += "                    break;" + _strNewLine;
            strCode += "                " + _strNewLine;
            strCode += "                case \"Delete" + dbTable.ClassName + "\":" + _strNewLine;
            strCode += "                    break;" + _strNewLine;
            strCode += "            }" + _strNewLine;
            strCode += "        }" + _strNewLine;

            strCode += "" + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// Opens the edit panel" + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        protected void OpenEditPanel()" + _strNewLine;
            strCode += "        {" + _strNewLine;
            strCode += "            lbnAddNew" + dbTable.ClassName + ".Visible = false;" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "            pnlEdit" + dbTable.ClassName + ".Visible = true;" + _strNewLine;
            strCode += "            pnl" + dbTable.ClassName + "List.Visible = false;" + _strNewLine;
            strCode += "        }" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// Opens the list panel" + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        protected void OpenListPanel()" + _strNewLine;
            strCode += "        {" + _strNewLine;
            strCode += "            lbnAddNew" + dbTable.ClassName + ".Visible = true;" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "            pnlEdit" + dbTable.ClassName + ".Visible = false;" + _strNewLine;
            strCode += "            pnl" + dbTable.ClassName + "List.Visible = true;" + _strNewLine;
            strCode += "        }" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// Update the user list" + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        protected void Update" + dbTable.ClassName + "List()" + _strNewLine;
            strCode += "        {" + _strNewLine;
            strCode += "            uc" + dbTable.ClassName + "List.DataSource = " + dbTable.ClassName + "BLL.GetListByIP(new " + dbTable.ClassName + "IP());" + _strNewLine;
            strCode += "            uc" + dbTable.ClassName + "List.DataBind();" + _strNewLine;
            strCode += "        }" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// The user has pressed to create a new user, open the edit panel" + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        protected void lbnAddNew" + dbTable.ClassName + "_Click(object sender, EventArgs e)" + _strNewLine;
            strCode += "        {" + _strNewLine;
            strCode += "            // Set user id to null so that a new user wil be created when saving" + _strNewLine;
            strCode += "            " + dbTable.ClassName + "Id = null;" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "            OpenEditPanel();" + _strNewLine;
            strCode += "        }" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// The user has pressed to save the user that is being edited/created" + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        protected void lbnSave" + dbTable.ClassName + "_Click(object sender, EventArgs e)" + _strNewLine;
            strCode += "        {" + _strNewLine;
            strCode += "            // Confirm that the user has been saved" + _strNewLine;
            strCode += "            pnl" + dbTable.ClassName + "Saved.Visible = true;" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "            Update" + dbTable.ClassName + "List();" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "            OpenListPanel();" + _strNewLine;
            strCode += "        }" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "        /// <summary>" + _strNewLine;
            strCode += "        /// The user has pressed to cancel editing/creating a user" + _strNewLine;
            strCode += "        /// </summary>" + _strNewLine;
            strCode += "        protected void lbnCancelSave" + dbTable.ClassName + "_Click(object sender, EventArgs e)" + _strNewLine;
            strCode += "        {" + _strNewLine;
            strCode += "            OpenListPanel();" + _strNewLine;
            strCode += "        }" + _strNewLine;
            strCode += "    }" + _strNewLine;
            strCode += "}" + _strNewLine;
            strCode += "" + _strNewLine;
            strCode += "" + _strNewLine;

            return strCode;
        }

        protected static DataTable GetRelationTable(string strTableName, bool distinct)
        {
            // Get table relations
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand("exec sp_fkeys @pktable_name = '" + strTableName + "'");

            string[] columnNameFk = new[] { "PKTABLE_NAME", "FKCOLUMN_NAME", "PKCOLUMN_NAME" };
            string[] columnNamePk = new[] { "FKTABLE_NAME", "FKCOLUMN_NAME", "PKCOLUMN_NAME" };
            string[] columnNameFinal = new[] { "TABLE", "COLUMN", "PKCOLUMN" };

            DataTable dataTablePk = db.ExecuteDataSet(dbCommand).Tables[0].DefaultView.ToTable(distinct, columnNamePk);
            dataTablePk.Columns[0].ColumnName = "TABLE";
            dataTablePk.Columns[1].ColumnName = "COLUMN";
            dataTablePk.Columns[2].ColumnName = "PKCOLUMN";

            dbCommand = db.GetSqlStringCommand("exec sp_fkeys @fktable_name = '" + strTableName + "'");

            DataTable dataTableFk = db.ExecuteDataSet(dbCommand).Tables[0].DefaultView.ToTable(distinct, columnNameFk);
            dataTableFk.Columns[0].ColumnName = "TABLE";
            dataTableFk.Columns[1].ColumnName = "COLUMN";
            dataTableFk.Columns[2].ColumnName = "PKCOLUMN";

            dataTableFk.Merge(dataTablePk);
            DataTable dataTableFinal = dataTableFk.DefaultView.ToTable(true, columnNameFinal);

            return dataTableFinal;
        }
    }
}
